package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.goods.entity.GoodsTagInfoEntity;
import com.yuanfeng.goods.mapper.GoodsTagInfoMapper;
import com.yuanfeng.goods.service.GoodsTagInfoService;
import com.yuanfeng.goods.vo.CommunityArticleTagVO;
import com.yuanfeng.goods.vo.CommunityTopicVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class GoodsTagInfoServiceImpl extends ServiceImpl<GoodsTagInfoMapper, GoodsTagInfoEntity>
    implements GoodsTagInfoService{

    @Override
    public ResponseResult updateCommunity(Map<String, Object> map) {
        this.baseMapper.updateCommunity(map);
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult insertCommunity(Map<String, Object> map) {
        this.baseMapper.insertCommunity(map);
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult deleteCommunity(Map<String, Object> map) {
        List<Integer> list= (List<Integer>) map.get("tagIdList");
        if(!CollectionUtils.isEmpty(list)){
            this.baseMapper.delete(new LambdaQueryWrapper<GoodsTagInfoEntity>()
                    .in(GoodsTagInfoEntity::getTagId,list));
        }

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult selectCommunity(Map<String, Object> map) {
        IPage<CommunityTopicVO> page=this.baseMapper.selectCommunity(new Query<CommunityTopicVO>().getPage(map),
                map);
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult queryCount(Map<Object, Object> param) {
        if(null != param ){
            Integer tagId = (Integer) param.get("tagId") ;
            Map<String,Object> map =  this.baseMapper.queryCount(tagId);
            return ResponseResult.success(map);
        }
        return ResponseResult.fail(0, "参数不能为null");
    }

    @Override
    public ResponseResult queryTags(Map<Object, Object> param) {
        List<CommunityArticleTagVO> tagList = this.baseMapper.queryTag(param);
        if (CollectionUtils.isEmpty(tagList)) {
            tagList = Lists.newArrayList();
        }
        return ResponseResult.success(tagList);
    }

    @Override
    public ResponseResult queryCommunity( Map<String, String> map) {
        Integer num = this.baseMapper.selectCount(new LambdaQueryWrapper<GoodsTagInfoEntity>()
                      .eq(GoodsTagInfoEntity::getIsActive, CommonType.commonStatus.YES.getCode()));
        List<GoodsTagInfoEntity> params = this.baseMapper.selectList(new LambdaQueryWrapper<GoodsTagInfoEntity>()
                .eq(GoodsTagInfoEntity::getIsActive, CommonType.commonStatus.YES.getCode())
                .orderByAsc(GoodsTagInfoEntity::getTagId).last("limit 6"));
        if (num>=6){
            return ResponseResult.success(params);
        }else {
            int limit = 6 - num;
            Map<Object,Object> tmpMap = new HashMap<>();
            tmpMap.put("limit",limit);
            List<GoodsTagInfoEntity> param = this.baseMapper.queryCommunityClassificationsLimit(tmpMap);
            param.addAll(params);
            return ResponseResult.success(param);
        }
    }
}




